package com.zhuojy.equity.mall.admin.web.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhuojy.equity.mall.admin.common.entity.DocumentClass;
import com.zhuojy.equity.mall.admin.common.pojo.ResultEntity;
import com.zhuojy.equity.mall.admin.common.utils.ExcelUtils;
import com.zhuojy.equity.mall.admin.service.DocumentClassService;
import com.zhuojy.equity.mall.admin.service.DocumentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.nimbus.State;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("documentClass")
@RequiredArgsConstructor
@Api(tags = "文档分类")
public class DocumentClassController {
    private final DocumentClassService documentClassService;
    @ApiOperation(value = "文档分类，分页查询", httpMethod = "POST", notes = "文档分类列表分页查询")
    @RequestMapping("queryDocumentClass")
    public ResultEntity queryDocumentClass(@RequestBody DocumentClass documentClass) {
        try {
            IPage page = documentClassService.queryDocumentClass(documentClass);
            return ResultEntity.OK(page);
        }catch (Exception e){
            e.printStackTrace();
            return ResultEntity.ERROR("查询失败");
        }

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("deleteDocumentClass")
    public ResultEntity deleteDocumentClass(@RequestParam (value = "id")Integer id){
        try {
            documentClassService.deleteDocumentClass(id);
            return ResultEntity.OK("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("操作失败");
        }

    }
    /**
     *新增类型
     */
    @RequestMapping("addDocumentClass")
    public ResultEntity  addDocumentClass(@RequestBody  DocumentClass documentClass){
         try {
             documentClassService.addDocumentClass(documentClass);
             return ResultEntity.OK("操作成功");
         } catch (Exception e) {
             e.printStackTrace();
             return ResultEntity.OK("操作失败");
         }
    }
    /**
     * 回显
     */
    @RequestMapping("selectDocumentClass")
    public ResultEntity  selectDocumentClass(@RequestParam (value = "id")Integer id){
        try {
           DocumentClass documentClass = documentClassService.selectDocumentClass(id);
            return ResultEntity.OK("操作成功"+documentClass);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.OK("操作失败");
        }
    }
    /**
     * 修改
     */
    @PostMapping("updateDocumentClass")
    public ResultEntity  updateDocumentClass(@RequestBody DocumentClass documentClass){
        try {
            documentClassService.updateDocumentClass(documentClass);
            return ResultEntity.OK("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.OK("操作失败");
        }
    }

    /**
     * 停用，停用
     */
    @PostMapping("updateStateDocumentClass")
    @ApiOperation("修改状态")
    public ResultEntity  updateStateDocumentClass(@RequestParam(value = "id")Long id,@RequestParam("state")Integer state){
        try {
            documentClassService.updateStateDocumentClass(id,state);
            return ResultEntity.OK("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.OK("操作失败");
        }
    }
    @ApiOperation(value = "导出", httpMethod = "POST", notes = "导出")
    @RequestMapping(value = "/export/excel")
    public void exportExcel(HttpServletResponse response) {
        documentClassService.exportExcel(response);
    }
    }
